//点击找相似事件
$(".attention .good-item .good-similarBtn .similarCon").click(function () {

    if ($(this).hasClass('active')) {
        var _this=this;
        $(this).parents('.good-line').next('.similar-line').slideUp(400,function(){
            $(_this).removeClass('active');
            $(_this).find('.iconfont').html("&#xe606");
        });

    } else {

        $(this).parents('.good-line').find(".good-similarBtn .similarCon").each(function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                $(this).find('.iconfont').html("&#xe606;");
            }
        });
        $(this).parents('.good-line').find(".good-similarBtn .similarCon").removeClass('active');
        $(this).addClass('active');
        $(this).find('.iconfont').html("&#xe610;");
        $(this).parents('.good-line').next('.similar-line').slideDown();
    }
});

//相似删除按钮
$('.attention .similar-line .similarPage i,.attention .similar-loading i').click(function () {
    $(this).parents('.similar-line').slideUp(400, function () {
        $(this).prev('.good-line').find(".good-item .good-similarBtn .similarCon").each(function () {
            if ($(this).hasClass('active')) {
                $(this).removeClass('active');
                $(this).find('.iconfont').html('&#xe606;');
            }
        });
    });

});

//相似商品滚轮（左翻页）
$('.attention .similar-line i.youfanye').click(function () {
    var curMarginLeft = parseInt($(this).parents('.similar-line').find('ul').css('marginLeft'));
    $(this).parents('.similar-line').find('ul').animate({marginLeft: (curMarginLeft - 930)});
    similarIcon('youfanye', this);
});
//右翻页
$('.attention .similar-line i.zuofanye').click(function () {
    var curMarginLeft = parseInt($(this).parents('.similar-line').find('ul').css('marginLeft'));
    $(this).parents('.similar-line').find('ul').animate({marginLeft: (curMarginLeft + 930)});
    similarIcon('zuofanye', this);
});

function similarIcon(param, that) {
    var curPageNo = parseInt($(that).parents('.similar-line').find('.similarPage span').text());
    var totalPageNo = parseInt($(that).parents('.similar-line').find('.similarPage b').text());
    if (param == 'youfanye') {
        $(that).parents('.similar-line').find('.similarPage span').text(++curPageNo);
    } else if (param == 'zuofanye') {
        $(that).parents('.similar-line').find('.similarPage span').text(--curPageNo);
    }

    $(that).parents('.similar-line').find('i.zuofanye').removeClass('none');
    $(that).parents('.similar-line').find('i.youfanye').removeClass('none');
    if (curPageNo == 1) {
        $(that).parents('.similar-line').find('i.zuofanye').addClass('none');
    }
    if (totalPageNo == curPageNo) {
        $(that).parents('.similar-line').find('i.youfanye').addClass('none');
    }
}

//点击置顶按钮
$('.attention .good-item .good-img .btn-top-div').click(function () {
    if($(this).children('span').text()=='设为置顶'){
        $(this).parent('.good-img').children('.hint-top').removeClass('none');
        $(this).children('i').addClass('color-red');
        $(this).children('span').text('取消置顶')
    }else if($(this).children('span').text()=='取消置顶'){
        $(this).parent('.good-img').children('.hint-top').addClass('none');
        $(this).children('i').removeClass('color-red');
        $(this).children('span').text('设为置顶');
    }
});

//点击删除按钮
$('.attention-good .good-item .good-btn .cartIcon').click(function(){
    $(this).parents('.good-item').find('.attention-layer-cover').removeClass('none');
    $(this).parents('.good-item').find('.attention-layer').removeClass('none');
});
//取消关注按钮的取消
$('.attention-good .layer-btns .btn-cancel').click(function(){
    $(this).parents('.good-item').find('.attention-layer-cover').addClass('none');
    $(this).parents('.good-item').find('.attention-layer').addClass('none');
});
//确定取消关注按钮
/*$('.attention-good .layer-btns .btn-confirm').click(function(){
	unfollowBySku(skuId);
});*/
//购物车提示
/*$('.attention-good .good-item .good-btn .good-cart').click(function(){
		window.$carToast.success($(this));
		//window.$carToast.fail($(this));
});*/

function addItemsBySku(skuId,obj) {
	console.log('加车');
	$.ajax({
		type : "POST",
		url : "/follow/addItemsBySku.do",
		data:{"itemNums":skuId},
		dataType : "json",
		success : function(result) {
			if(null != result){
				if(result.code == 1){
					window.$carToast.success($(obj));
					//window.location.href = "http://home.yhd.com/follow/getFollowProductList.do";
				}else{
					window.$carToast.fail($(obj));
					//window.location.href = "//item.yhd.com/" + skuId + ".html";
				}
			}else{
				window.$carToast.fail($(obj));
				//window.location.href = "//item.yhd.com/" + skuId + ".html";
			}
		}
	});
}
function unfollowBySku(skuId) {
	$.ajax({
		type : "POST",
		url : "/follow/UnFollowProduct.do",
		data:{"productId":skuId},
		dataType : "json",
		success : function(result) {
			if(result!=null){
				if(result =="success"){
					window.location.href = "//home.yhd.com/follow/getFollowProductList.do"
				}
			}
		}
	});
}

function dop(pd){
		window.location.href="//home.yhd.com/follow/getFollowProductList.do?pageNo="+pd;
	}
var _myFollowProductObject = {
/**
 * 加载分页栏
 */
	loadPgBar : function () {
		// 当前页
		var currentPage = $("#currentPage").val();
		// 页大小
		var pageSize = $("#pageSize").val();
		// 订单总数量
		var totalCount = $("#totalCount").val();
		//alert("init-param, currentPage=" + currentPage + ", pageSize=" + pageSize + ", totalCount=" + totalCount);
		//alert(1);
		if (!currentPage || !pageSize || !pageSize) return;
		currentPage = parseInt(currentPage);
		//alert(2);
		pageSize = parseInt(pageSize);
		totalCount = parseInt(totalCount);
		
		// 计算页数量
		var totalPage = parseInt((totalCount + pageSize - 1)/pageSize + "");
		//alert(3);
		//alert("totalPage=" + totalPage);
		// 只有一页，不显示分页
	    // if (totalPage <= 1) return;
	    
	    // 分页栏显示8个分页元素
		var item = new Array(8);
		
		// 当页的数量小于等于8个：依次显示8个分页元素
		if (totalPage <= 8) {
			for (var i=0; i<totalPage; i++) {
				item[i] = i+1;
			}
		} else {
			// 当前页两边对称
			if (currentPage <= 4) {
				item[0] = 1; 
				item[1] = 2;
				item[2] = 3;
				item[3] = 4;
				item[4] = 5;
				item[5] = 6;
				item[6] = 7;
				item[7] = "...";
				item[8] = totalPage;
			}
			else if (currentPage > 4 && currentPage < totalPage - 4) {
				item[0] = currentPage - 3; 
				item[1] = currentPage - 2;
				item[2] = currentPage - 1;
				item[3] = currentPage;
				item[4] = currentPage + 1;
				item[5] = currentPage + 2;
				item[6] = currentPage + 3;
				item[7] = "...";
				item[8] = totalPage;
			}
			else {
				item[0] = 1;
				item[1] = "...";
				item[2] = totalPage - 6;
				item[3] = totalPage - 5
				item[4] = totalPage - 4;
				item[5] = totalPage - 3;
				item[6] = totalPage - 2;
				item[7] = totalPage - 1;
				item[8] = totalPage;
			}
		}
	
		var pgBarContent = "";
		
		// 向前样式
		// 第1页向前按钮置灰
		if (1 == currentPage) {
			pgBarContent += "<span class=\"page_start\"><i class=\"iconSearch\">&#xe61f;</i>上一页</span>";
		} else {
			pgBarContent += "<a class=\"page_prev\"><i class=\"iconSearch\">&#xe61f;</i>上一页</a>";
		}
		
		// 分页导航元素样式
		for (var i=0; i<item.length; i++) {
			if (!item[i]) break;
			if (currentPage == item[i]) {
				pgBarContent += "<span class=\"page_cur\">" + currentPage + "</span>";
			}
			else if ("..." == item[i]) {
				pgBarContent += "<span class=\"page_break\">...</span>";
			} 
			else {
				pgBarContent += "<a class=\"page_nav_item\">" + item[i] + "</a>";
			}
		}
		
		// 向后样式
		// 最后一页向后按钮置灰
		if (totalPage == currentPage) {
			pgBarContent += "<span class=\"page_end\">下一页<i class=\"iconSearch\">&#xe60b;</i></span>";
		} else {
			pgBarContent += "<a class=\"page_next\">下一页<i class=\"iconSearch\">&#xe60b;</i></a>";
		}
		//alert("pgBarContent:"+pgBarContent);
		$(".turnPageBottom").html('');
		//alert(4);
		//$(".turnPageBottom").html('<div>1</div>');
		//alert(5);
		$(".turnPageBottom").html(pgBarContent);
		//alert(6);
		// -----------------------------绑定事件   begin----------------------------------
		// 向前事件
		$(".page_prev").bind("click", function(){
			var pagePrev = currentPage - 1;
			console.log("pagePrev=" + pagePrev);
			if (pagePrev > 0) {
				dop(pagePrev);
			}
		});
		
		// 向后事件
		$(".page_next").bind("click", function(){
			var pageNext = currentPage + 1;
			console.log("pageNext=" + pageNext);
			if (pageNext <= totalPage) {
				dop(pageNext);
			}
		});
		
		// 某一页元素加载
		$(".page_nav_item").bind("click", function(){
			var currentPage = $(this).text();
			dop(currentPage);
		});
		
		// -----------------------------绑定事件      end----------------------------------
		},
		initFun: function() {
	        this.loadPgBar();				// 初始化关注商品列表分页工具栏
	    }
}
$(document).ready(function() {
	_myFollowProductObject.initFun();
});
